package com.hh.service.service.impl.pagemanagement;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.enums.BannerPosition;
import com.hh.common.enums.LinkType;
import com.hh.common.exception.HhException;
import com.hh.common.model.pagemanagement.Banner;
import com.hh.common.request.pagemanagement.BannerSaveRequest;
import com.hh.common.response.pagemanagement.BannerFrontResponse;
import com.hh.common.response.pagemanagement.BannerResponse;
import com.hh.common.response.pagemanagement.BannerTabsHeader;
import com.hh.common.token.FrontTokenComponent;
import com.hh.service.dao.CategoryDao;
import com.hh.service.dao.JingangAreaDao;
import com.hh.service.dao.StoreProductDao;
import com.hh.service.dao.pagemanagement.BannerDao;
import com.hh.service.service.SystemAttachmentService;
import com.hh.service.service.pagemanagement.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerDao, Banner> implements BannerService {

    @Resource
    private BannerDao dao;

    @Resource
    private StoreProductDao storeProductDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private JingangAreaDao jingangAreaDao;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private FrontTokenComponent frontTokenComponent;




    @Override
    public List<BannerResponse> getAdminList(Integer position) {
        LambdaQueryWrapper<Banner> bannerQueryWrapper = Wrappers.lambdaQuery();
        bannerQueryWrapper.eq(Banner::getPosition, position);
        bannerQueryWrapper.eq(Banner::getIsDel, 0);
        List<Banner> banners = this.dao.selectList(bannerQueryWrapper);
        List<BannerResponse> list = new ArrayList<>();
        if (banners.size() > 0) {
            for (Banner banner : banners) {
                BannerResponse bannerResponse = new BannerResponse();
                BeanUtil.copyProperties(banner, bannerResponse);
                list.add(bannerResponse);
            }
        }
        return list;
    }

    @Override
    public Boolean save(BannerSaveRequest request) {
        Banner banner = new Banner();
        banner.setIsShow(request.getIsShow());
        banner.setImgUrl(this.systemAttachmentService.clearPrefix(request.getImgUrl()));
        banner.setLinkType(request.getLinkType());
        banner.setLinkUrl(request.getLinkUrl());
        banner.setPosition(request.getPosition());
        return this.dao.insert(banner) > 0;
    }

    @Override
    public Banner getInfo(Integer id) {
        return this.dao.selectById(id);
    }

    @Override
    public Boolean update(BannerSaveRequest request) {
        Integer id = request.getId();
        Banner info = this.getInfo(id);
        if (ObjectUtil.isNull(info)) {
            throw new HhException("该轮播图不存在");
        }
        Banner banner = new Banner();
        BeanUtil.copyProperties(request, banner);
        banner.setId(id);
        banner.setImgUrl(this.systemAttachmentService.clearPrefix(banner.getImgUrl()));
        return this.updateById(banner);
    }

    @Override
    public boolean display(Integer id) {
        Banner banner = this.dao.selectById(id);
        banner.setIsShow(1);
        return this.updateById(banner);
    }

    @Override
    public boolean hide(Integer id) {
        Banner banner = this.dao.selectById(id);
        banner.setIsShow(0);
        return this.updateById(banner);
    }

    @Override
    public List<BannerFrontResponse> getList(Integer position, Integer limit) {
        String merId = frontTokenComponent.getMerId();

        LambdaQueryWrapper<Banner> bannerQueryWrapper = Wrappers.lambdaQuery();
        bannerQueryWrapper.eq(Banner::getPosition, position);
        bannerQueryWrapper.eq(Banner::getIsShow, 1);
        bannerQueryWrapper.eq(Banner::getIsDel, 0);
        bannerQueryWrapper.eq(Banner::getMerId,merId);
        bannerQueryWrapper.last(" limit " + limit);
        List<Banner> banners = this.dao.selectList(bannerQueryWrapper);
        List<BannerFrontResponse> bannerFrontResponses = new ArrayList<>();
        if (banners.size() > 0) {
            for (Banner banner : banners) {
                BannerFrontResponse bannerFrontResponse = new BannerFrontResponse();
                BeanUtil.copyProperties(banner, bannerFrontResponse);
                bannerFrontResponses.add(bannerFrontResponse);
            }
        }
        return bannerFrontResponses;
    }

    @Override
    public boolean deleteBanner(Integer id) {
        Banner banner = this.dao.selectById(id);
        banner.setIsDel(1);
        return this.updateById(banner);
    }

    @Override
    public List<BannerTabsHeader> getTabsHeader() {
        List<BannerTabsHeader> headers = new ArrayList<>();
        BannerTabsHeader header1 = new BannerTabsHeader(BannerPosition.APPHOMEPAGE.getInfo(), BannerPosition.APPHOMEPAGE.getCode());
        BannerTabsHeader header3 = new BannerTabsHeader(BannerPosition.PRODUCTCATE.getInfo(), BannerPosition.PRODUCTCATE.getCode());
        headers.add(header1);
        headers.add(header3);
        return headers;
    }


    private void checkBannerLink(Integer linkType, Integer linkUrl) {
        if (linkType.equals(LinkType.PRODUCT.getCode())) {
            int count = this.storeProductDao.selectCountByBanner(linkUrl);
            if (count < 1) {
                throw new HhException("该商品不存在");
            }
        } else {
            int count = this.categoryDao.selectCountByBanner(linkUrl);
            if (count < 1) {
                throw new HhException("该分类不存在");
            }
        }
    }
}
